#include "reg52.h"

sbit L1 = P0^0;
sbit S7 = P3^0;

unsigned char count = 0;
unsigned char stat_LED = 0;
unsigned char Level = 0;

void InitT0()
{
	TMOD = 0x01;
	TH0 = (65535 - 100) / 256;
	TL0 = (65535 - 100) % 256;

	EA = 1;
	ET0 = 1;
}

void ServeT0()	interrupt 1
{
	TH0 = (65535 - 100) / 256;
	TL0 = (65535 - 100) % 256;
	count++;
	if (count == stat_LED)
	{
		L1 = 1;
	}

	else if (count == 100)
	{
		L1 = 0;
		count = 0;
	}
}
void DelayKey(unsigned int t)
{
	while (t--);
}

void ScanKey()
{
	if (S7 == 0)
	{
		DelayKey(100);
		if (S7 == 0)
		{
			switch (Level)
			{
			case 0:
				L1 = 0;
				TR0 = 1;
				stat_LED = 10;
				Level = 1;
				break;

			case 1:
				stat_LED = 50;
				Level = 2;
				break;

			case 2:
				stat_LED = 90;
				Level = 3;
				break;

			case 3:
				TR0 = 0;
				L1 = 1;
				Level = 0;
				break;
			}
			while (S7 == 0);
		}
	}
}

void main()
{
	P2 = (P2 & 0x1f) | 0x80;
	L1 = 1;
	InitT0();

	while (1)
	{
		ScanKey();
	}
}